SPELL = {}
Spells = {}
	
SPELL.__index = SPELL

AccessorFunc( SPELL, "m_sName", "Name", FORCE_STRING )
AccessorFunc( SPELL, "m_sToolTip", "ToolTip", FORCE_STRING )
AccessorFunc( SPELL, "m_sIcon", "Icon", FORCE_STRING )
AccessorFunc( SPELL, "m_iManaCost", "ManaCost", FORCE_NUMBER )
AccessorFunc( SPELL, "m_iRange", "Range", FORCE_NUMBER )
AccessorFunc( SPELL, "m_iSpellID", "SpellID", FORCE_NUMBER )
AccessorFunc( SPELL, "m_iCoolDown", "Cooldown", FORCE_NUMBER )
AccessorFunc( SPELL, "m_bRequiresTarget", "RequiresTarget", FORCE_BOOL )
AccessorFunc( SPELL, "m_bRecastWhileCasting", "RecastWhileCasting", FORCE_BOOL )
AccessorFunc( SPELL, "m_iSpellType", "SpellType", FORCE_NUMBER )

SPELL_INVALID = -1

SPELL.spellID = 0
SPELL.Modifiers = {}

SPELL.Modifiers.testmod = {	 
							name = "Test Cleave Level 1", 
							desc = "Performs an exact copy of the current spell on two random enemies within 5 yards of the target."
							}
							
function SPELL.Modifiers.testmod:func(s, pl, target)
	local closestTargets = EnemiesInRange(pl, target, 240)
	--pl:EmitSound("devin/blackbetty/weapons/ohbabyatriple.wav", 100, 100+math.random(-20,20))
		for i = 1, 2, 1 do
			target = table.Random(closestTargets)
			if(target != nil)then
				s:Effect(pl, target);
				table.remove(closestTargets, target);
			end
		end
end

print("Acquiring spells...")

local _R = debug.getregistry( )
	_R.Spell = SPELL
local n, m

local curindex = 1

function SPELL:CanCast( pl )
	return pl:GetMana( ) >= self:GetManaCost( )
end

function SPELL:GetSpellID()
	return self:GetSpellID() or SPELL_INVALID
end

function SPELL:GetSpellName()
	return self:GetName()
end

function SPELL:GetSpellToolTip()
	return self:GetToolTip()
end

function SPELL:CreateNPC(id, pos)
	local foundID = false
	local npc = ents.Create("npc_rpgdm")
	npc:SetPos(pos)
	for k, v in ipairs(NPCs)do
		if(NPCs[k].MobID == id)then
			print(NPCs[k].MobID)
			NPCs[k]:ApplyToEnt(npc)
			foundID = true
			break
		end
	end
	if(foundID == false)then
		print("Could not find NPC of ID#"..id..".")
		return false
	end
	npc:Spawn()
	npc:Activate()
end

function SPELL:ApplySpellMods( s, pl, target )
	--PrintTable(s)
	if(target != nil)then
		for k, v in pairs(self.Modifiers) do
			v:func(s, pl, target)
		end
	end
end

function SPELL:Cast( pl, target )
end

function SPELL:Effect( pl, target )
end

function SPELL:DealDamage(minimum, maximum, attacker, target, dmgtype)
	if(SERVER)then
		local dmginfo = DamageInfo()
		
		dmginfo:SetDamage( math.Round(math.random(minimum, maximum)))
		dmginfo:SetDamageType( dmgtype )
		dmginfo:SetAttacker( attacker )
		dmginfo:SetDamageForce( Vector( 0, 0, 1000 ) )
		
		target:TakeDamageInfo( dmginfo )
	end
end

for n, m in ipairs( file.Find( "gamemodes/RPGDM/gamemode/spells/*.lua", "MOD" ) ) do
	
	print( "spells/" .. m)
	AddCSLuaFile( "spells/" .. m )
	include( "spells/" .. m)
		
	local SPELL = table.Copy(_R.Spell)
	
	table.insert( Spells, SPELL )
		curindex = curindex + 1
end

